package com.qijian.maindata.tools.poi;

import cn.hutool.core.collection.CollectionUtil;
import com.qijian.common.utils.poi.ExcelCustomizationTemplateAdapter;
import com.qijian.maindata.domain.Item;
import com.qijian.maindata.domain.LineProcessItem;
import com.qijian.maindata.domain.vo.BomDetailVo;
import com.qijian.maindata.domain.vo.BomVo;
import com.qijian.maindata.domain.vo.LineProcessVo;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

/**
 * @author gw
 */
public class ExcelCustomizationBomMaterial extends ExcelCustomizationTemplateAdapter {

    public ExcelCustomizationBomMaterial() {
        super("材料跟踪表.xls");
    }

    @Override
    public String writeSheet(Object obj) {
        if (obj == null) {
            return "";
        }
        BomVo d = (BomVo) obj;
        Item bomItem = d.getItem();
        String result = "";
        if (bomItem != null) {
            Row row2 = getRow(2);
            Cell productName = row2.getCell(2);
            productName.setCellValue(bomItem.getItemName());
            Cell bomNo = row2.getCell(7);
            bomNo.setCellValue(bomItem.getItemCode());

            Row row4 = getRow(4);
            Cell drawingNo = row4.getCell(3);
            drawingNo.setCellValue(bomItem.getDrawingNo());

            result = bomItem.getItemName();
        }

        if (CollectionUtil.isNotEmpty(d.getBomDetails())) {
            int start = 7;
            for (BomDetailVo bomDetail : d.getBomDetails()) {
                String thickness = findItemAttr(bomDetail.getItemAttr(), "厚");
                String itemName = bomDetail.getItemName();
                if (CollectionUtil.isEmpty(bomDetail.getProcessList())) {
                    continue;
                }
                for (LineProcessVo process : bomDetail.getProcessList()) {
                    if (CollectionUtil.isEmpty(process.getProcessItemList())) {
                        continue;
                    }
                    insertRow(start, process.getProcessItemList().size(), 12);
                    for (LineProcessItem processItem : process.getProcessItemList()) {
                        Row temp = getRow(start++);
                        Cell temp_cell0 = getCell(temp, 0);
                        temp_cell0.setCellValue(bomDetail.getDetailCode());
                        Cell temp_cell1 = getCell(temp, 1);
                        temp_cell1.setCellValue(bomDetail.getDrawingNo());
                        Cell temp_cell3 = getCell(temp, 3);
                        temp_cell3.setCellValue(processItem.getItemName());
                        Cell temp_cell5 = getCell(temp, 5);
                        if (itemName != null && itemName.contains("数控件")) {
                            temp_cell5.setCellValue(thickness);
                        } else {
                            temp_cell5.setCellValue(findItemAttr(processItem.getRemark(), "规格"));
                        }

                        Cell temp_cell7 = getCell(temp, 7);
                        temp_cell7.setCellValue(findItemAttr(processItem.getRemark(), "材质"));
//                        Cell temp_cell9 = getCell(temp, 9);
                    }

                }

            }
        }
        return result;
    }


}
